/*
 * @Author: sunzhijun 
 * @Date: 2020-02-28 21:36:16 
 * @Last Modified by: sunzhijun
 * @Last Modified time: 2020-03-12 16:59:28
 */

import { cacheTools } from 'nc-lightapp-front';
import CommonAction from './common';
export default class tableAction extends CommonAction{
    constructor(comp) {
        super();
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    //获取table数据
    getTableData = async ( isRefresh, conditions )=>{
        const { props, action } = this.comp;
        const { editTable, button, form, main } = props;
        let { appConfig: { appcode, pagecode }, pk_org, tableId, pageInfo, isCardForm } = main;
        action.btnAct.buttonStatus();
        let res = await this.dispatch({
            type: 'main/getTableAction',
            payload: {
                pageCode: pagecode,
                appCode: appcode,
                pk_org: pk_org,
                areaCode: tableId,
                pageInfo: pageInfo,
                ...conditions
            }
        });
        if( res.success ){
            let data = res.data;
            let rowsData,allpks;
            allpks = data && data.dataList && data.dataList.allpks || [];
            cacheTools.set('allpks', allpks);
            if(!!data){
                if (!isCardForm) {
                    rowsData = data && data.dataList.rows;
                    editTable.setTableData(tableId, { rows: rowsData || [] });
                    button.setButtonDisabled({
                        edit: false,
                        // add: true,
                        submit: false,
                        query: false,
                        delete: false,
                        launch: false,
                        history: false,
                        revoke: false,
                        // refresh: true,
                        export_excel: false
                    });
                    //设置翻页数据
                    this.dispatch({
                        type: 'main/update',
                        payload: {
                            pageInfo: data.dataList.pageInfo,
                            isVisiblePagination: true   //控制翻页显示隐藏
                        }
                    })
                }
            }else{
                button.setButtonDisabled({
                    // edit: false,
                    // add: true,
                    submit: true,
                    query: false,
                    delete: true,
                    launch: true,
                    history: true,
                    revoke: true,
                    // refresh: true,
                    export_excel: true
                });
                editTable.setTableData(tableId, { rows: [] });
            }
            if (!!isRefresh && isRefresh === 'refresh') {
                this.toast({ color: 'success', content: language['hrcm-000016'] }); /*国际化：刷新成功 */
            }
        } else {
            editTable.setTableData(tableid, { rows: [] });
            form.EmptyAllFormValue(cardId);
            if (isDataListPage) {
                button.setButtonDisabled(['refresh'], false);
                button.setButtonVisible(['query'],true);
            } else {
                button.setButtonDisabled(['refresh'], true);
                button.setButtonVisible(['query'],false);
            }
        }
    }

    // 进入卡片页
    toCardForm = async ( record )=>{
        const { action } = this.comp;
        let pk_ctrtmng;
        if( !!record ){
            pk_ctrtmng = record.values ? record.values.pk_ctrtmng.value : record;
        }
        await this.dispatch({
            type: 'main/update',
            payload: {
                isCardForm: true,
                isShowOrg: false,
                pk_ctrtmng: pk_ctrtmng,
                isCardPaginationVisible: true
            }
        })
        action.cardAct.getFormData();
    }
}